package com.endava.graduates;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;

public class BenchmarkIO {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("rand.txt");

		FileChannel fc = fos.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(10000);

		Random r = new Random(System.currentTimeMillis());

		byte[] byteArray;

		long currentLength = 0;
		while (currentLength < 1024 * 1024 * 5) {
			if (1024 * 1024 * 5 - currentLength < 10000) {
				byteArray = new byte[(int) (1024 * 1024 * 5 - currentLength)];
			} else {
				byteArray = new byte[10000];
			}
			r.nextBytes(byteArray);
			buffer.put(byteArray);
			buffer.flip();
			fc.write(buffer);

			buffer.clear();

			currentLength += 10000;
		}

		System.out.println("done!");

		
		
		BenchmarkIO benk = new BenchmarkIO();
		
		benk.readWithBF();
		
		long currentTime = System.currentTimeMillis();
		for (int i = 0; i < 50; i++) {

			benk.readWithNio();
			System.out.println(" ");
			
		}

		long currentTime2 = System.currentTimeMillis();
		
		 System.out.println(currentTime2 - currentTime);
		 
		 
		/**
		 * This is reading the file with BufferedReader 50 times 10000 byte
		 * chunk at a time.And shows the time it takes to execute in
		 * milliseconds.
		 */

		 fos.close();
		 fc.close();
		 

	}
	public void readWithBF() throws IOException{
		System.out.println("Please wait until the for statement finish executing...");
		System.out.print("The value is: ");
		long currentTimeNio = System.currentTimeMillis();
		int readLength;
		for (int i = 0; i < 50; i++) {
			FileReader fileReader = new FileReader("rand.txt");
			BufferedReader bufReader = new BufferedReader(fileReader);
			do {
				char[] charBuffer = new char[10000];
				readLength = bufReader.read(charBuffer);
				
				if (readLength != -1) {
					for (int j = 0; j < 10; j++) {
						// System.out.println(charBuffer[j]);
					}
				}
			} while (readLength != -1);
			bufReader.close();
		}
		long currentTimeNio2 = System.currentTimeMillis();
		
		System.out.println(currentTimeNio2 - currentTimeNio);
		
	}

	public void readWithNio() throws IOException {

		FileInputStream fin = new FileInputStream("rand.txt");
		FileChannel fChannel = fin.getChannel();
		ByteBuffer buffeReader = ByteBuffer.allocate(1024 * 1024 * 5);
		fChannel.read(buffeReader);

		byte[] byteArray = new byte[1024 * 1024 * 5];
		buffeReader.flip();

		buffeReader.get(byteArray);

		for (int x = 0; x < 10; x++) {
			System.out.print(byteArray[x]);
		}

		buffeReader.clear();

		fin.close();
		fChannel.close();

	}

}

// byte[] message = { 'H', 'o', 'w', 'd', 'y', '!' };

// for (int i = 0; i < 1024; i++) {
// buffer.put((byte) alphabet.charAt(r.nextInt(N)));
//
//
// }
// final String alphabet = "0123456789ABCDE";
// final int N = alphabet.length();
// for (int i = 0; i < message.length; i++) {
// buffer.put(message[i]);
// }

// buffer.flip();
// fc.write(buffer);